<?php
/* @var $this CategoriesController */
/* @var $model Categories */
/* @var $form TbActiveForm */
?>
<link rel="stylesheet" href="<?php echo Yii::app()->theme->baseUrl; ?>/assets/css/magicsuggest-1.3.1.css">
<script type="text/javascript" src="<?php echo Yii::app()->theme->baseUrl; ?>/assets/js/magicsuggest-1.3.1.js"></script>
<div class="form">
	<?php
	$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
		'id' => 'categories-form',
		// Please note: When you enable ajax validation, make sure the corresponding
		// controller action is handling ajax validation correctly.
		// There is a call to performAjaxValidation() commented in generated controller code.
		// See class documentation of CActiveForm for details on this.
		'enableAjaxValidation' => true,
		'clientOptions' => array(
			'validateOnSubmit' => true,
		),
	));
	?>
	<?php echo $form->errorSummary($model); ?>

	<?php echo $form->dropDownListControlGroup($model, 'pageParent', $model->getDropdownItems(), array('empty' => 'нет', 'encode' => false, 'id' => 'source', 'style' => 'width:40%')); ?>    

	<?php echo $form->textFieldControlGroup($model, 'pageTitle', array('span' => 5, 'maxlength' => 255)); ?>

	<?php echo $form->textFieldControlGroup($model, 'pageUrl', array('span' => 5, 'maxlength' => 255)); ?>

	<?php
	$select = !$model->isNewRecord ? 'model' : '';
	if (!$model->isNewRecord) {
		$a = explode(',', $model->pageLinkedServices);
		$select = Page::model()->findByPk((int) $a[0])->pageType;
	} else {
		$select = '';
	}
	?>
	<div class="control-group">
		<label for="Categories_pageUrl" class="control-label required">Прикреплённые страницы</label>
		<div class="controls">
			<?php
			echo CHtml::dropDownList('type', $select, Controller::$ratingItems, array('empty' => '--',
				'ajax' => array(
					'type' => 'POST',
					'url' => CController::createUrl('categories/getData'),
					'update' => '#result',
			)));
			echo!$model->isNewRecord ? CHtml::hiddenField('value', $model->pageLinkedServices) : '';
			?>
		</div>
	</div>
	<div id="result"></div>

	<div class="form-actions">
		<?php
		echo TbHtml::submitButton($model->isNewRecord ? 'Create' : 'Save', array(
			'color' => TbHtml::BUTTON_COLOR_PRIMARY,
			'size' => TbHtml::BUTTON_SIZE_LARGE,
		));
		?>
    </div>
	<script>
		$(document).ready(function() {
<?php if (!$model->isNewRecord): ?>
				$.post('<?php echo CController::createUrl('categories/getData'); ?>', {type: $('#type').val(), value: $('#value').val()}, function(response) {
					$('#result').html(response);
				});
<?php endif; ?>
			$('#Categories_pageTitle').keyup(function() {
				$('#Categories_pageUrl').val(cyr2lat($(this).val()));
			});
		});
	</script>

<?php $this->endWidget(); ?>

</div><!-- form -->